﻿using Net.Sz.Framework.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net.Sz.Framework.ExcelTools.CreateCode.excel.text
{
    /// <summary>
    /// 
    /// <para>PS:</para>
    /// <para>@author 失足程序员</para>
    /// <para>@Blog http://www.cnblogs.com/shizuchengxuyuan/</para>
    /// <para>@mail 492794628@qq.com</para>
    /// <para>@phone 13882122019</para>
    /// </summary>
    public class CreateCSV : CreateBase
    {
        static readonly CreateCSV instance = new CreateCSV();
        public static CreateCSV Instance()
        {
            return instance;
        }

        protected override void CreateClassFile(string fileName, string msg)
        {
            base.CreateClassFile(TextPath + "/" + fileName + ".csv", msg);
        }

        public void ActionExcel(ExcelDatas exceldatas, string cellgs, Action<string> show)
        {
            try
            {
                HashSet<string> tablenames = new HashSet<string>();
                foreach (var itemkey in exceldatas.FileName_And_SheetName.Keys)
                {
                    var datas = exceldatas.Datas[itemkey];

                    string sheetName = datas.SheetName;
                    string fileName = itemkey;
                    if (!tablenames.Add(sheetName))
                    {
                        continue;
                    }
                    StringBuilder builder = new StringBuilder();
                    List<ExcelData.ExcelDataRow.ExcelDataCell> cellNames = new List<ExcelData.ExcelDataRow.ExcelDataCell>();
                    var rows = datas.Rows.Values.ToArray();
                    bool isdouhao = false;
                    {
                        /*写入标题*/
                        var cells = rows[0].Cells;
                        foreach (var item in cells)
                        {
                            if (item.Value.Cells.Equals("ALL") || cellgs.Equals("ALL") || item.Value.Cells.Equals(cellgs))
                            {
                                cellNames.Add(item.Value);
                                if (isdouhao) { builder.Append(","); }
                                actionIn(builder, item.Value.CellName);
                                isdouhao = true;
                            }
                        }
                        builder.AppendLine();
                    }
                    isdouhao = false;
                    {
                        /*写入第二的注释*/
                        var cells = rows[1].Cells;
                        for (int k = 0; k < cellNames.Count; k++)
                        {
                            ExcelData.ExcelDataRow.ExcelDataCell cellK = cellNames[k];
                            ExcelData.ExcelDataRow.ExcelDataCell cell = null;
                            foreach (var item in cells)
                            {
                                if (item.Value.CellName.Equals(cellK.CellName))
                                {
                                    cell = item.Value;
                                    break;
                                }
                            }
                            if (cell == null)
                            {
                                cell = cellK;
                            }
                            if (isdouhao) { builder.Append(","); }
                            actionIn(builder, cell.CellNotes);
                            isdouhao = true;
                        }
                        builder.AppendLine();
                    }
                    /*写入数据*/
                    isdouhao = false;
                    for (int i = 0; i < rows.Length; i++)
                    {
                        var cells = rows[i].Cells;
                        isdouhao = false;
                        for (int k = 0; k < cellNames.Count; k++)
                        {
                            ExcelData.ExcelDataRow.ExcelDataCell cellK = cellNames[k];
                            ExcelData.ExcelDataRow.ExcelDataCell cell = null;
                            foreach (var item in cells)
                            {
                                if (item.Value.CellName.Equals(cellK.CellName))
                                {
                                    cell = item.Value;
                                    break;
                                }
                            }
                            if (cell == null)
                            {
                                cell = cellK;
                            }
                            if (isdouhao) { builder.Append(","); }
                            actionIn(builder, cell.CellValue);
                            isdouhao = true;

                        }
                        builder.AppendLine();
                    }
                    CreateClassFile(fileName, builder.ToString());
                }
            }
            catch (Exception ex)
            {
                show("请仔细检查配置表 错误信息：" + ex.ToString());
            }
        }

        void actionIn(StringBuilder sb, string context)
        {
            if (context.Length==0)
            {
                return;
            }
            if (context.IndexOf("\"") == 0)
            {
                context = "\"" + context;
            }
            if (context.IndexOf("\"") == context.Length - 1)
            {
                context = context + "\"";
            }
            if (context.Contains("\"") || context.Contains(","))
            {
                sb.Append("\"").Append(context).Append("\"");
            }
            else
            {
                sb.Append(context);
            }
        }

    }
}
